<?php

namespace haibao\cms\data\emode;

use haibao\cms\model\filter\activity\Activity as ActivityFilter;

class EmodeProduct extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\emode\EmodeProduct');
    }
    
    public function saveEmodeProduct($model){
        if($model->Id){
            $this->save($model);
        }else{
            $this->add($model);
        }
        
    }
    
    public function getProductCount(){
        $sql = 'select b.Id as brandId,count(a.Id) as num from EmodeProduct as a left join EmodeBrand as b on a.BrandId = b.Id group by b.Id';
        $result = $this->query($sql);
        $productCount = array();
        while($row = $result->fetch_assoc()){
            $productCount[$row['brandId']] = $row['num'];
        }
        return $productCount;
    }
    
    public function getEmodeProductList($query){
        return $this->getAll($query);
    }
    
    public function getEmodeProductById($productId){
        $sql = 'select a.*,b.Name from EmodeProduct as a left join EmodeBrand as b on a.BrandId = b.Id where a.Id = %s and a.Status = %s';
        return $this->query($sql,array($productId,\haibao\cms\model\data\emode\EmodeProduct::STATUS_ENABLE))->fetch_assoc();
    }
    
    public function setProducStatus($id,$status){
        $sql = 'update EmodeProduct set Status = %s where Id = %s';
        $this->query($sql,array($status,$id));
    }
    
}